const db = require("../dbUtil/index");
const sql = require("../dbUtil/sql");
const functionCtl = require("./function/function");

module.exports = {
    //歌曲收藏
    songLike: (req, res) => {
        let { songID, userID } = req.query;
        let like = 0;
        db.query(sql.likes.songLike2, [userID, songID], (err, results) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                res.send({
                    code: 505,
                    msg: "数据库执行错误！",
                    data: {}
                });
            } else {
                if (results.length  == 0) {
                    db.query(sql.likes.songLike, [songID, userID], (err, results2) => {
                        if (err) {
                            //返回一个json对象：状态码、状态描述、数据
                            res.send({
                                code: 505,
                                msg: "数据库执行错误！",
                                data: {}
                            });
                        } else {
                            //获取数据库数据
                            res.send({
                                code: 200,
                                msg: "收藏成功！",
                                data: {}
                            });
                        }
                    })
                } else {
                    like += 1;
                    res.send({
                        code: 200,
                        msg: "已收藏",
                        data: { like }
                    });
                }
            }
        })

    },
    //歌单收藏
    songListLike: (req, res) => {
        let { userID, songListName } = req.query;
        let like = 0;
        db.query(sql.likes.songListLike2, [userID, songListName], (err, results2) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                res.send({
                    code: 505,
                    msg: "数据库1执行错误！",
                    data: {}
                });
            } else {
                if (results2.length == 0) {
                    db.query(sql.likes.songListLike, [songListName,userID], (err, results) => {
                        if (err) {
                            //返回一个json对象：状态码、状态描述、数据
                            res.send({
                                code: 505,
                                msg: "数据库2执行错误！",
                                data: {}
                            });
                        } else {
                            //获取数据库数据
                            res.send({
                                code: 200,
                                msg: "收藏成功！",
                                data: {}
                            });
                        }
                    })
                } else {
                    like += 1;
                    //获取数据库数据
                    res.send({
                        code: 200,
                        msg: "已收藏！",
                        data: { like }
                    });
                }
            }
        })
    },
    //歌曲取消收藏
    songCancel: (req, res) => {
        let { songID, userID } = req.query;
        db.query(sql.likes.songCancel, [songID, userID], (err, results) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                res.send({
                    code: 505,
                    msg: "数据库执行错误！",
                    data: {}
                });
            } else {
                //获取数据库数据
                res.send({
                    code: 200,
                    msg: "取消收藏！",
                    data: {}
                });
            }
        })
    },
    //歌单取消收藏
    songListCancel: (req, res) => {
        let { userID, songListName } = req.query;
        db.query(sql.likes.songListCancel, [userID, songListName], (err, results) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                res.send({
                    code: 505,
                    msg: "数据库执行错误！",
                    data: {}
                });
            } else {
                //获取数据库数据
                res.send({
                    code: 200,
                    msg: "取消收藏歌单！",
                    data: {}
                });
            }
        })
    },
    //收藏的全部歌曲
    songLikes: (req, res) => {
        let { userID } = req.query;
        db.query(sql.likes.songLikes, [userID], (err, results) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                res.send({
                    code: 505,
                    msg: "数据库执行错误！",
                    data: {}
                });
            } else {
                var songLikes = [];
                var songList = results[0];
                for (var i in results) {
                    function s() {
                        return new Promise((resolve, reject) => {
                            db.query(sql.likes.songLikes2, [results[i].songID], (err2, results2) => {
                                if (err2) {
                                    reject(err2);
                                } else {
                                    resolve(results2);
                                }
                            })
                        })
                    }
                    s().then((results2) => {
                        var singerName = [];
                        var singerID = [];
                        for (var j in results2) {
                            singerName.push(results2[j].singerName);
                            singerID.push(results2[j].singerID);
                        }
                        results2[0].singerName = singerName;
                        results2[0].singerID = singerID;
                        //时长
                        var arr1 = results2[0].songWord.split("[");
                        var arr2 = arr1[arr1.length - 1].split("]");
                        var time = arr2[0].substring(1, 5);
                        results2[0].time = time;
                        songLikes.push(results2[0]);
                        if (songLikes.length == results.length) {
                            //获取数据库数据
                            res.send({
                                code: 200,
                                msg: "数据库执行成功！",
                                data: { songLikes, songList }
                            });
                        }
                    }).catch((err2) => {
                        //返回一个json对象：状态码、状态描述、数据
                        return res.send({
                            code: 505,
                            msg: "数据库2执行错误！",
                            data: {}
                        });
                    })
                }
            }
        })
    },
    //收藏的全部歌单
    songListlikes: (req, res) => {
        let { songList_uid } = req.query;
        db.query(sql.likes.songListLikes, [songList_uid], (err, results) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                res.send({
                    code: 505,
                    msg: "数据库执行错误！",
                    data: {}
                });
            } else {
                var songListLikes = [];
                for (var i in results) {
                    db.query(sql.likes.songListLikes4, [results[i].songListName], (err3, results3) => {
                        if (err3) {
                            res.send({
                                code: 505,
                                msg: "数据库执行错误！",
                                data: {}
                            });
                        } else {
                            songListLikes.push(results3);
                            if (songListLikes.length == results.length) {
                                res.send({
                                    code: 200,
                                    msg: "数据库执行成功！",
                                    data: { data: songListLikes }
                                })
                            }
                        }
                    })
                }
            }
        })
    },
}